//
//  LoginViewController.m
//  RZIM
//
//  Created by reyzhang on 2021/8/12.
//

#import "LoginViewController.h"
#import "UIViewController+HUD.h"
#import <MJExtension/MJExtension.h>
#import "YMApiManager.h"
#import <Masonry/Masonry.h>
@interface LoginViewController ()<UITextFieldDelegate>

@property (nonatomic,strong)UILabel *titleLable;
@property (nonatomic,strong)UIView *phoneView;
@property (nonatomic,strong)UITextField *userTF;

@property (nonatomic,strong)UIView *codeView;
@property (nonatomic,strong)UITextField *nicknameTF;

@property (nonatomic,strong)UIButton *enterButton;
@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationController setNavigationBarHidden:YES];
    
    [self initView];
}

-(void)initView{
    
    [self.view addSubview:self.titleLable];
    [self.titleLable mas_makeConstraints:^(MASConstraintMaker *make) {
            
        make.top.mas_equalTo(125);
        make.left.mas_equalTo(30);
        
    }];
    
    [self.view addSubview:self.nicknameTF];
    [self.nicknameTF mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.mas_equalTo(self.view.mas_left).offset(30);
        make.top.mas_equalTo(self.titleLable.mas_bottom).offset(55);
        make.right.mas_equalTo(self.view.mas_right).offset(-19);
        make.height.mas_equalTo(35);
        
    }];
    
    [self.view addSubview:self.phoneView];
    [self.phoneView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.mas_equalTo(self.nicknameTF.mas_left);
        make.height.mas_equalTo(1);
        make.width.mas_equalTo(self.nicknameTF.mas_width);
        make.top.mas_equalTo(self.nicknameTF.mas_bottom).offset(5);
            
    }];
 
    [self.view addSubview:self.userTF];
    [self.userTF mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.mas_equalTo(self.nicknameTF.mas_left);
        make.top.mas_equalTo(self.phoneView.mas_bottom).offset(35);
        make.width.mas_equalTo(self.nicknameTF.mas_width);
        make.height.mas_equalTo(35);
        
    }];
    
    [self.view addSubview:self.codeView];
    [self.codeView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.mas_equalTo(self.titleLable.mas_left);
        make.height.mas_equalTo(1);
        make.width.mas_equalTo(self.userTF.mas_width);
        make.top.mas_equalTo(self.userTF.mas_bottom).offset(5);
            
    }];
  

 
    [self.view addSubview:self.enterButton];
    [self.enterButton mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.mas_equalTo(self.view.mas_left).offset(25);
        make.right.mas_equalTo(self.view.mas_right).offset(-25);
        make.top.mas_equalTo(self.codeView.mas_bottom).offset(100);
        make.height.mas_equalTo(46);
        
    }];
    


}
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self.userTF resignFirstResponder];
    [self.nicknameTF resignFirstResponder];
    
}



- (void)loginAction{
    
    if (self.userTF.text.length == 0) {
        [self showHint:@"请输入用户唯一id"];
        return;
    }
    
    if (self.nicknameTF.text.length == 0) {
        [self showHint:@"请输入用户昵称"];
        return;
    }
    
    [self showLoading];
    [YMApiManager userLoginWithUserId:self.userTF.text name:self.nicknameTF.text success:^(NSDictionary * _Nonnull dic) {
        
        //get token
        NSString *token = dic[@"data"][@"token"];
        [[NSUserDefaults standardUserDefaults] setObject:token forKey:@"token"];
        [[NSUserDefaults standardUserDefaults] setObject:self.userTF.text forKey:@"userId"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"enterMain" object:nil];
        
        
    } failure:^(NSError * _Nonnull error) {
        [self hideHud];
    }];
    
}
-(UILabel *)titleLable{
    
    if (_titleLable == nil) {
        
        _titleLable = [[UILabel alloc]init];
        _titleLable.text = @"您好，\n欢迎来到直播Live";
        _titleLable.font = [UIFont boldSystemFontOfSize:23];
        _titleLable.textColor = [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1];
        _titleLable.numberOfLines = 0;
        
    }
    
    return _titleLable;
}
-(UIView *)phoneView{
    
    if (_phoneView == nil) {
        
        _phoneView = [[UIView alloc]init];
        _phoneView.backgroundColor = [UIColor colorWithRed:242/255.0 green:243/255.0 blue:247/255.0 alpha:1.0];
        _phoneView.layer.cornerRadius = 10;
        _phoneView.clipsToBounds = YES;
    }
    
    return _phoneView;
}

-(UITextField *)userTF{
    
    if (_userTF == nil) {
        
        _userTF = [[UITextField alloc]init];
        _userTF.font = [UIFont systemFontOfSize:16];
        _userTF.textColor = [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1];
        _userTF.keyboardType = UIKeyboardTypeNumberPad;
        _userTF.delegate = self;
        // 就下面这两行是重点
        _userTF.placeholder = @"请输入用户唯一id";

    
    }
    
    return _userTF;
}
-(UIView *)codeView{
    
    if (_codeView== nil) {
        
        _codeView = [[UIView alloc]init];
        _codeView.backgroundColor = [UIColor colorWithRed:242/255.0 green:243/255.0 blue:247/255.0 alpha:1.0];
        _codeView.layer.cornerRadius = 10;
        _codeView.clipsToBounds = YES;
    }
    
    return _codeView;
}
-(UITextField *)nicknameTF{
    
    if (_nicknameTF == nil) {
        
        _nicknameTF = [[UITextField alloc]init];
        _nicknameTF.font = [UIFont systemFontOfSize:16];
        _nicknameTF.textColor = [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1];
        _nicknameTF.keyboardType = UIKeyboardTypeDefault;
        _nicknameTF.delegate = self;
        _nicknameTF.placeholder = @"请输入用户昵称";

    }
    
    return _nicknameTF;
}



-(UIButton *)enterButton{
    
    if (_enterButton == nil) {
        
        _enterButton = [[UIButton alloc]init];
        _enterButton.backgroundColor = [UIColor colorWithRed:51/255.0 green:133/255.0 blue:255/255.0 alpha:1];
        [_enterButton setTitle:@"进入" forState:UIControlStateNormal];
        [_enterButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _enterButton.titleLabel.font = [UIFont systemFontOfSize:15];
        [_enterButton addTarget:self action:@selector(loginAction) forControlEvents:UIControlEventTouchUpInside];
        _enterButton.layer.cornerRadius = 23;
    }
    
    return _enterButton;
}



@end
